Skip to content

Return non-routine callable attributes when called without arguments - #238

Merged
kou merged 1 commit into
red-data-tools:masterfrom
felixmarch:claude/fix-issue-87-callable-attr-04972b
Nov 11, 2026
Merged

kou merged 1 commit into
red-data-tools:masterfrom
felixmarch:claude/fix-issue-87-callable-attr-04972b

Conversation

@felixmarch

Copy link
Copy Markdown
Contributor

Fixes #87.

Problem

PyCall.import_module('scipy.stats').norm.pdf([1, 2, 3], 0, 10) raises

TypeError: rv_continuous_frozen.pdf() takes 2 positional arguments but 4 were given

while the same expression works in Python.

scipy.stats.norm is not a class or a function but a callable instance of norm_gen. Ruby cannot distinguish stats.norm from stats.norm(), and pycall_pyobject_wrapper_wrapper_method calls every callable attribute, so stats.norm.pdf(x, 0, 10) is evaluated as stats.norm().pdf(x, 0, 10): pdf of a frozen distribution (loc=0, scale=1), which only takes x.

Change

When an attribute is accessed without arguments, it is called only if it is a routine. Otherwise the callable object is returned as is. Calls with arguments are unchanged.

"Routine" follows Python's own definition in inspect.isroutine(), implemented in C with type checks (no Python-level call per access):

  • builtin_function_or_method, bound method, and method-wrapper (and subtypes)
  • non-data descriptors: types that define __get__ but not __set__. This covers Python functions, method descriptors, slot wrappers, Cython functions (cython_function_or_method), functools.lru_cache wrappers, numpy's _ArrayFunctionDispatcher (np.sum, etc.), and so on.

Two additions to keep existing code working:

  • functools.partial: it is a routine on Python 3.14+ (it gained __get__), and it is special-cased so that the behavior does not depend on the Python version.
  • Ruby Proc/Method objects stored in a Python attribute are still called, as before.

Class objects are still returned as is, as before.

Behavior change

With no arguments, these are now returned instead of being called:

Attribute Before After
scipy.stats.norm and other rv instances called: frozen distribution the generator object
numpy ufuncs (np.sin, scipy.special.gamma) called with no args: TypeError the ufunc object
other instances of classes defining __call__, operator.itemgetter(...) called the object
a non-callable Ruby object stored in a Python attribute TypeError: non-callable ruby object the Ruby object

Everything else is unchanged. I checked these against real numpy/scipy: np.get_include, np.random.random, np.random.default_rng (Cython), ndarray.sum/copy/tolist, staticmethods and classmethods are all still called without arguments.

Risk: code that relies on obj.callable_instance calling it with zero arguments now gets the object back. It can use obj.callable_instance.() (or .call) instead. The README section "The callable attribute of an object" now documents this.

Tests

  • New spec/pycall/callable_attribute_spec.rb with a scipy-like helper module spec/python/pycall/callable_attribute.py (no scipy needed). It covers the callable-instance case (no args, positional args, keyword args, .(), class/instance attributes). It also checks that zero-argument functions, bound methods, staticmethods, classmethods, builtin methods and functions, method-wrappers, lru_cache wrappers, partial objects, callable non-data descriptors, and Ruby lambdas are still called.
  • Without the C change, the 4 callable-instance specs fail. With it, the full suite passes: 212 examples, 0 failures, 27 pending.
  • Tested on Windows Server 2025, RubyInstaller Ruby 3.4 (UCRT), Anaconda Python 3.14.6, numpy 2.x and scipy 1.18. To load Python there, I used a local workaround for the Windows finder (the issue addressed by Fix PyCall::PythonNotFound on Windows #236); it is not part of this PR.

Not included

  • pyfrom ..., import: :norm (lib/pycall/import.rb) still defines a method that calls any non-class callable, so the same issue remains there.

🤖 Generated with Claude Code

Ruby cannot distinguish `obj.attr` from `obj.attr()`, so PyCall used to
call every callable attribute.  This broke objects like scipy.stats.norm,
a callable instance of norm_gen: `stats.norm.pdf(x, 0, 10)` became
`stats.norm().pdf(x, 0, 10)` and raised TypeError (red-data-toolsGH-87).

Now, when no argument is given, a callable attribute is called only if
it is a routine in the sense of Python's inspect.isroutine(): a builtin
function or method, a bound method, a method-wrapper, or a non-data
descriptor (Python functions, method descriptors, Cython functions,
functools.lru_cache wrappers, numpy's array function dispatchers, etc.).
functools.partial objects, and Ruby Proc/Method objects stored in Python
attributes, are also treated as routines.  Other callable objects, such
as instances of classes defining __call__ and numpy ufuncs, are returned
as they are.  Calls with arguments are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@kou kou left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

@mrkn This includes a backward incompatibility but I think that it's reasonable. What do you think about it?

@mrkn

mrkn commented Nov 11, 2026

Copy link
Copy Markdown
Member

@kou I agree with you, but we might need to bump the version to 2.0.0 when we release this change.

@kou

kou commented Nov 11, 2026

Copy link
Copy Markdown
Member

OK. Let's use "2.0.0" for the next version.

@kou
kou merged commit 4f288e0 into red-data-tools:master Nov 11, 2026
41 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

scipy.stats.norm.pdf isn't usable from PyCall

3 participants